<?php

/**
 * Challenge Cup Teams system file,
 * @author Luke Barratt 
 * @copyright Copyright (C) 2008 by Luke Barratt
 * @license http://www.gnu.org/copyleft/gpl.html
 */

defined('CC_VALID') or 
die('Direct Access to this location is not allowed.'); 

// Game/Team listing variables

$CC_GAME_ID = $_GET['game_id'];
$CC_TEAM_ID = $_GET['team_id'];

echo "<div id=\"teams\">";

if ($CC_GAME_ID == "") 
{
	$CC_GAMELIST_QUERY = mysql_query('SELECT * FROM cc_game');
	while ($CC_GAMELIST_ARRAY = mysql_fetch_array($CC_GAMELIST_QUERY)) 
	{
		echo 
		"<div class=\"bar\">
		<a href=\"index.php?mod=teams&game_id=". $CC_GAMELIST_ARRAY['game_id'] ."\" >". $CC_GAMELIST_ARRAY['game_name'] ."</a>
		</div>";
	}
	CC_FREE_RESULT($CC_GAMELIST_QUERY);
}
else
{
	if ($CC_GAME_ID != "" && $CC_TEAM_ID == "") 
	{
		$CC_TEAMS_QUERY = mysql_query("SELECT * FROM cc_teams, cc_join WHERE cc_teams.team_id = cc_join.join_id_parent AND cc_join.join_type = 'game' AND cc_join.join_id_link = '{$CC_GAME_ID}' ORDER BY cc_teams.team_name ASC");
		while ($CC_TEAMS_ARRAY = mysql_fetch_array($CC_TEAMS_QUERY)) 
		{	
			$CC_GAME_ID = $_GET['game_id'];
			echo 
			"<div class=\"bar\">
				<div class=\"team\">
				<img src=\"images/nation/". $CC_TEAMS_ARRAY['team_nation'] .".gif\"/>
					<a href=\"index.php?mod=teams&game_id=". $CC_GAME_ID ."&team_id=". $CC_TEAMS_ARRAY['team_id'] ."\" >". $CC_TEAMS_ARRAY['team_name'] ."</a>
				</div>
				<div class=\"www\">
					<a href=\"http://". $CC_TEAMS_ARRAY['team_www'] ."\">". $CC_TEAMS_ARRAY['team_www'] ."</a>
				</div>
			</div>";
		}
		CC_FREE_RESULT($CC_TEAMS_QUERY);
	}
	
	if ($CC_GAME_ID != "" && $CC_TEAM_ID != "") 
	{
		$CC_GAME_ID = $_GET['game_id'];
		$CC_TEAM_INFO_QUERY = mysql_query("SELECT * FROM cc_teams WHERE team_id = '{$CC_TEAM_ID}'");
		$CC_TEAM_INFO_ARRAY = mysql_fetch_array($CC_TEAM_INFO_QUERY);
		$CC_PLAYER_INFO_QUERY = mysql_query("SELECT * FROM cc_teams, cc_join, cc_users WHERE cc_teams.team_id = '{$CC_TEAM_ID}' AND cc_users.user_id = cc_join.join_id_link AND cc_join.join_type = 'team' AND cc_join.join_id_parent = cc_teams.team_id");

		echo
		"<div class=\"bar\">
			<div class=\"team\">
			<img src=\"images/nation/". $CC_TEAM_INFO_ARRAY['team_nation'] .".gif\"/> ". $CC_TEAM_INFO_ARRAY['team_name'] ."
			</div>
			<div class=\"www\">
				<a href=\"http://". $CC_TEAM_INFO_ARRAY['team_www'] ."\">". $CC_TEAM_INFO_ARRAY['team_www'] ."</a>
			</div>
		</div> 
		<div id=\"leftinfo\">
		Roster:<br/><br/>";
			
		while ($CC_PLAYER_INFO_ARRAY = mysql_fetch_array($CC_PLAYER_INFO_QUERY)) 
		{	
			echo "<img src=\"images/nation/". $CC_PLAYER_INFO_ARRAY['user_nation'] .".gif\"/> 
			<a href=\"index.php?mod=players&game_id=". $CC_GAME_ID ."&user_id=". $CC_PLAYER_INFO_ARRAY['user_id'] ."\">". $CC_PLAYER_INFO_ARRAY['user_name'] ."</a>
			<br/>";	
		}
		$CC_PLAYER_INFO_ROWS = mysql_num_rows($CC_PLAYER_INFO_QUERY);
		if ($CC_PLAYER_INFO_ROWS == NULL) {
			echo "<center>-FOLDED TEAM-</center>";
		}
		
		echo
		"</div>
		<div id=\"rightinfo\">
			Rightinfo
		</div>";

		CC_FREE_RESULT($CC_PLAYER_INFO_QUERY);
	}
}

echo "</div>";

?>